package GUI;

import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import baseClass.Arquivo;

/**
 *
 * @author kaiooliveira
 */
public class janelaCadastroVotos extends javax.swing.JFrame {

	private static String saidaVotacoes;
	private static String arqEstabelecimentos;
	private static String arqOpinioes;
	private int i = 0;
	private boolean condicao = true;
	private String stringNotas = "";
	
	private String[][] nomesDosEstabelecimentosNotas  = new String[39][2];
	
	/**
	 * Formatar a hora
	 * @return
	 * 		a hora formatada
	 */
	public static String hora() {

		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"dd/MM/yyyy hh:mm:ss");
		String hora = dateFormat.format(new Date());

		return hora;

	}
	
	/**
	 * 
	 * @param nomesEstabelecimentos
	 * @param indice
	 * @param nota
	 * @param permiteColocarZero
	 * @return
	 */
	public String[][] EstabelecimentosNotas(String[] nomesEstabelecimentos, int indice, String nota, boolean permiteColocarZero){
		
		
		
		for (int i = 0; i < nomesEstabelecimentos.length; i++) {
			if (indice == i){
				nomesDosEstabelecimentosNotas[i] = new String[] {nomesEstabelecimentos[i], nota+""};
			
			}else{
				if(permiteColocarZero){
					nomesDosEstabelecimentosNotas[i] = new String[] {nomesEstabelecimentos[i], "0: Nao conheco"};
				}
				
			}
			

		}
		
		return nomesDosEstabelecimentosNotas;
		
	}
	
	public String[] retornaListaEstabelecimentos(String arquivo) {

		// List<String> nomeDosEstabelecimentos = new ArrayList<String>();
		Arquivo a = new Arquivo();
		try {
			a.lerArquivo(arqEstabelecimentos);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String[] nomeDosEstabelecimentos = new String[a
				.getLinhasDoArquivoLido().size() - 1];
		for (int i = 1; i < a.getLinhasDoArquivoLido().size(); i++) {

			nomeDosEstabelecimentos[i - 1] = (a.getLinhasDoArquivoLido().get(i)
					.split(";")[0]);
		}
		return nomeDosEstabelecimentos;

	}
	
    /** Creates new form NewJFrame1 */
	public janelaCadastroVotos(String rest, String opini) {
		this.arqEstabelecimentos = rest;
		this.arqOpinioes = opini;
		initComponents();
	}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupoBotoesNotas = new javax.swing.ButtonGroup();
        jDialog1 = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        nota11 = new javax.swing.JRadioButton();
        nota10 = new javax.swing.JRadioButton();
        nota9 = new javax.swing.JRadioButton();
        nota8 = new javax.swing.JRadioButton();
        nota7 = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaRestaurantesNotas = new javax.swing.JTable();
        nota3 = new javax.swing.JRadioButton();
        nota4 = new javax.swing.JRadioButton();
        nota5 = new javax.swing.JRadioButton();
        nota1 = new javax.swing.JRadioButton();
        nota6 = new javax.swing.JRadioButton();
        nota2 = new javax.swing.JRadioButton();
        caixaRestaurantes = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        org.jdesktop.layout.GroupLayout jDialog1Layout = new org.jdesktop.layout.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        jLabel1.setText("Lista de Restaurantes");

        nota11.setText("-5: Detesto");
        grupoBotoesNotas.add(nota11);

        nota10.setText("-4: Acho muito ruim");
        grupoBotoesNotas.add(nota10);

        nota9.setText("-3: Acho bastante ruim");
        grupoBotoesNotas.add(nota9);

        nota8.setText("-2: Acho pouco ruim");
        grupoBotoesNotas.add(nota8);

        nota7.setText("-1: Acho pouco ruim");
        grupoBotoesNotas.add(nota7);

        tabelaRestaurantesNotas.setModel(new javax.swing.table.DefaultTableModel(
        		EstabelecimentosNotas(retornaListaEstabelecimentos(arqEstabelecimentos), 0,"0: Nao conheco",condicao),
            new String [] {
                "Nome do Restaurante", "Nota"
            }
        ));
        jScrollPane1.setViewportView(tabelaRestaurantesNotas);

        nota3.setText("3: Bastante bom");
        grupoBotoesNotas.add(nota3);

        nota4.setText("4: Muito bom");
        grupoBotoesNotas.add(nota4);

        nota5.setText("5: Incrivel, sensacional, impressionante");
        grupoBotoesNotas.add(nota5);

        nota1.setText("1: Eh bonzinho");
        grupoBotoesNotas.add(nota1);

        nota6.setText("0: Nao conheco");
        grupoBotoesNotas.add(nota6);
        nota6.setSelected(true);

        nota2.setText("2: Eh bonzinho");
        grupoBotoesNotas.add(nota2);

        caixaRestaurantes.setModel(new javax.swing.DefaultComboBoxModel(retornaListaEstabelecimentos(arqEstabelecimentos)));

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 0, 18));
        jLabel2.setText("Notas");

        jButton1.setText("Votar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	botaoVotarActionPerformed(evt);
            }
        });

        jButton2.setText("Cadastrar votos permanentemente");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	cadastraPermActionPerformed(evt);
            }
        });

        jButton3.setText("Cadastrar votos temporariamente");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	cadastraTempActionPerformed(evt);
            }
        });

        jButton4.setText("Entenda os diferentes tipos de votos");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	botaoEntendaActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 401, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(17, 17, 17)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(22, 22, 22)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(nota4)
                            .add(nota7)
                            .add(nota2)
                            .add(nota3)
                            .add(nota6)
                            .add(nota9)
                            .add(nota8)
                            .add(nota10)
                            .add(nota11)
                            .add(nota1)
                            .add(layout.createSequentialGroup()
                                .add(50, 50, 50)
                                .add(jLabel2))
                            .add(nota5))
                        .add(39, 39, 39)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(caixaRestaurantes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 413, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(108, 108, 108)
                                .add(jLabel1)
                                .add(119, 119, 119)))
                        .addContainerGap(34, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(74, 74, 74)
                                .add(jButton1)
                                .add(300, 300, 300))
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 147, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(98, 98, 98)))
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jButton4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .add(64, 64, 64))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(32, 32, 32)
                                .add(nota1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(nota2)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(nota3)
                                .add(7, 7, 7)
                                .add(nota4)
                                .add(7, 7, 7)
                                .add(nota5)
                                .add(7, 7, 7)
                                .add(nota6)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(nota7)
                                .add(8, 8, 8)
                                .add(nota8)
                                .add(6, 6, 6)
                                .add(nota9)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(nota10)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(nota11))
                            .add(jLabel2)
                            .add(layout.createSequentialGroup()
                                .add(jLabel1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(caixaRestaurantes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(4, 4, 4)
                                .add(jButton4))
                            .add(jButton1))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(jButton3)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jButton2)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoEntendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoEntendaActionPerformed
    	new janelaCadastroAjuda().setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_botaoEntendaActionPerformed

    private void cadastraTempActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastraTempActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cadastraTempActionPerformed

    private void cadastraPermActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cadastraPermActionPerformed
    	
    	for (int i = 0; i < nomesDosEstabelecimentosNotas.length; i++) {
			if(i==nomesDosEstabelecimentosNotas.length - 1){
				stringNotas = stringNotas + (nomesDosEstabelecimentosNotas[i][1]);
			}
			else{
				stringNotas = stringNotas + (nomesDosEstabelecimentosNotas[i][1]) + ";";
			}
    		
    		
		}
    	
    	try {
			this.dispose();
			saidaVotacoes = hora() + ";" + janelaCadastroNome.opinioes[0] + ";"
					+ stringNotas;
			
			a.escreverEmArquivo(saidaVotacoes, arqOpinioes);
			new janelaCadastroProgresso(arqEstabelecimentos,arqOpinioes).setVisible(true);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }//GEN-LAST:event_cadastraPermActionPerformed

    private void botaoVotarActionPerformed(ActionEvent evt) {
        
    	condicao = false;
    	String notaDada = "0: Nao conheco";
    	
    	if (nota1.isSelected()){
            notaDada = "1: Eh bonzinho";
        }
    	else if (nota2.isSelected()){
            notaDada = "2: Eh bonzinho";
        }
    	else if (nota3.isSelected()){
            notaDada = "3: Bastante bom";
        }
    	else if (nota4.isSelected()){
            notaDada = "4: Muito bom";
        }
    	else if (nota5.isSelected()){
            notaDada = "5: Incrivel, sensacional, impressionante";
        }
    	else if (nota6.isSelected()){
            notaDada = "0: Nao conheco";
        }
    	else if (nota7.isSelected()){
            notaDada = "-1: Acho um pouco ruim";
        }
    	else if (nota8.isSelected()){
            notaDada = "-2: Acho um pouco ruim";
        }
    	else if (nota9.isSelected()){
            notaDada = "-3: Acho bastante ruim";
        }
    	else if (nota10.isSelected()){
            notaDada = "-4: Acho muito ruim";
        }
    	else if (nota11.isSelected()){
            notaDada = "-5: Detesto";
        }
        

     
    	tabelaRestaurantesNotas.setModel(new javax.swing.table.DefaultTableModel(
        		EstabelecimentosNotas(retornaListaEstabelecimentos(arqEstabelecimentos), caixaRestaurantes.getSelectedIndex() ,notaDada, condicao),
            new String [] {
                "Nome do Restaurante", "Nota"
            }
        ));
    	
    	
    	tabelaRestaurantesNotas.repaint();
    	
    	
    	
    }                                           

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new janelaCadastroVotos(arqEstabelecimentos, arqOpinioes).setVisible(true);
            }
        });
    }
    
    Arquivo a = new Arquivo();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup grupoBotoesNotas;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox caixaRestaurantes;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton nota1;
    private javax.swing.JRadioButton nota10;
    private javax.swing.JRadioButton nota11;
    private javax.swing.JRadioButton nota4;
    private javax.swing.JRadioButton nota5;
    private javax.swing.JRadioButton nota6;
    private javax.swing.JRadioButton nota7;
    private javax.swing.JRadioButton nota8;
    private javax.swing.JRadioButton nota9;
    private javax.swing.JRadioButton nota2;
    private javax.swing.JRadioButton nota3;
    private javax.swing.JTable tabelaRestaurantesNotas;
    // End of variables declaration//GEN-END:variables
}
